<?php
include 'cabecalho.php';
?>
<div class="container">
    <div class="row">
        <div class="col-md-11 text-left">
            <div class="navbar navbar-default">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand">Produtos</a>
                </div>
                <div class="collapse navbar-collapse navbar-ex1-collapse">
                </div>
            </div>
        </div>
    </div>
</div>

<div class="container">
    <div class="row">
        <div class="col-md-24"></div>
        <div class="col-md-20">
            <div class="collapse navbar-collapse navbar-ex1-collapse ">
                <?php
                if (empty($_SESSION['carrinho'])) {
                    echo '<h3>Carrinho Vazio</h3>';
                    echo "<br>";
                    echo "<hr>";
                    echo "<button type=\"submit\" class=\"btn btn-success\" disabled=\"true\">Finalizar Compra</button>";
                    echo "<button type=\"reset\" class=\"btn btn-danger\" disabled=\"true\">Limpar</button>";
                } else {
                    $codigos = "";
                    foreach ($_SESSION['carrinho'] as $produto => $quantidade) {
                        $codigos = $codigos . ',' . $produto;
                    }
                    $codigos = substr($codigos, 1);
                    $objPedido = new Carrinho();
                    $lista = $objPedido->pedido($codigos);

                    $saida = "";
                    $total = 0;
                    if (isset($lista)) {        // verifica se existe algum objeto
                        foreach ($lista as $item) {
                            $saida .= '<tr>
                              <td>' . $item->nome . '</td>
                              <td>' . $item->descricao . '</td>
                              <td>' . $item->tamanho . '</td>
                              <td>' . $_SESSION['carrinho'][$item->idproduto] . '</td>
                              <td>R$ ' . $item->valorP . '</td>
                              </tr>';
                            $total += $item->valorP * $_SESSION['carrinho'][$item->idproduto];
                        }
                    }
                    ?>
                    <form action="carrinhoFinalizar.php" metod="post">
                        <div class="container">
                            <div class="col-md-10 text-left">
                                <div class="table-responsive">
                                    <table class="table table-bordered table-hover table-striped table-condensed">
                                        <thead>
                                            <tr>
                                                <th><a href="#">Item</a></th>
                                                <th><a href="#">Detalhes</a></th>
                                                <th><a href="#">Tamanho</a></th>
                                                <th><a href="#">Quantidade</a></th>
                                                <th><a href="#">Valor</a></th>

                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php echo $saida; ?>
                                            <tr><td></td><td></td><td></td>
                                                <td>Total</td>
                                                <td><?php echo"R$ " . $total; ?></td>

                                            </tr>
                                        </tbody>

                                    </table>
                                </div>
                            </div>
                        </div>
                        <?php
                        echo "<br>";
                        echo "<hr>";
                        echo "<button type =\"submit\" class =\"btn btn-success\">Finalizar Compra</button>";
                        echo "<input type=\"button\" class=\"btn btn-danger\" onclick=\"limpa();\" value=\"Limpar\">";
                    }
                    ?>
                    <input type="button" class="btn btn-default" onclick="volta();
                           " value="Voltar" />
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    function volta() {
        window.location = "interna.php";
    }
    function limpa() {
        window.location = "carrinhoLimpar.php";
    }
</script>
<!--  Diálogos com o usuário -->
<?php
include 'rodape.php';
